package com.inspire.ijc.agent.asminstru;

import com.inspire.ijc.agent.IjcAgent;
import com.inspire.ijc.agent.storage.AllContext;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * @author ：inspire
 * @date ：Created in 2023/4/13 10:30
 * @description：
 * @modified By：inspire
 * @version:
 */
public class ProbeClassVisitor extends ClassVisitor implements Opcodes{
    String className;
    boolean classIdExist;
    public ProbeClassVisitor(int api, ClassVisitor cv, String className,boolean classIdExist) {
        super(api, cv);
        this.className=className;
        this.classIdExist=classIdExist;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
//        System.out.println("visitMethod access:" + access + ", name:" + name + ", desc:" + desc + ", signature:" + signature+",exceptions:"+exceptions);
        MethodVisitor mv =  cv.visitMethod(access, name, desc, signature, exceptions);
        if(IjcAgent.filterMethod_V.contains(name)){
            return mv;
        }

        int methodID;
        String methodPosition=className+"."+name+":"+desc;
        if(classIdExist){
            methodID=AllContext.METHOD_IDS.get(methodPosition);
        }else{
            methodID=AllContext.METHOD_ID_AUTO++;
            AllContext.METHOD_IDS.put(methodPosition,methodID);
        }

        mv = new ProbeMethodVisitor(ASM7,mv, access, name, desc,className, methodID);
        return mv;
    }


    public void visitEnd() {
        if (cv != null) {
            cv.visitEnd();
        }
    }
}
